package com.bing.spring;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by kaibb on 15-9-23.
 */

public class UserCrawler {

    private BufferedWriter writer = null;

    public static void main(String[] args) throws IOException, InterruptedException {

        UserCrawler crawler = new UserCrawler();
        crawler.CreateWriter("/home/kaibb/Downloads/1.txt");   //输出文件路径
        crawler.read("/home/kaibb/Downloads/memberList.txt");  //输入文件路径


    }

    /**
     *
     * @param fstr
     * @throws IOException
     * @throws InterruptedException
     */
    private void crawlPages(String fstr) throws IOException, InterruptedException {
        String num = null;
        String input_line = null;
        String link = "http://www.dianping.com/member/" + fstr;
        System.out.println(link);
        int pos1 = -1;
        int pos2 = -1;
        URL url = new URL(link);
        URLConnection con = url.openConnection();

        con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36");

//	  System.getProperties().put("proxySet", "true");  //以下3句加上去下载特别慢，有没有快一些的方法
//	  System.getProperties().put("proxyHost", "218.14.227.197");
//	  System.getProperties().put("proxyPort", "3128");

        BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));

        while ((input_line = rd.readLine()) != null) {
            //	  System.out.println(input_line);
            if (input_line.contains("col-link\">点评(")) {
                pos1 = input_line.indexOf("col-link\">点评(");
                pos2 = input_line.indexOf(")", pos1);
                num = input_line.substring(pos1 + 13, pos2);
                System.out.println(fstr + " " + num);

                writer.write(fstr + " " + num + "\n");
                writer.flush();
                Thread.sleep(2000);
                break;
            }
        }


    }

    public void read(String inputPath) throws IOException, InterruptedException {

        BufferedReader in = new BufferedReader(new FileReader(inputPath));
        String fstr;

        while ((fstr = in.readLine()) != null) {
            crawlPages(fstr);

        }
        in.close();
    }

    private void CreateWriter(String filename) {
        try {
            File myFile = new File(filename);
            FileWriter fileWriter = new FileWriter(myFile, true);
            writer = new BufferedWriter(fileWriter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}